package cn.clwxy.gdz.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
public class BaseDao {
	private Connection conn;
	private QueryRunner queryRunner=new QueryRunner();
	public int update(String sql,Object...obj){
		conn=ConnectionUtil.getConnection();
		try {
			return queryRunner.update(conn,sql, obj);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ConnectionUtil.clossAll(conn);
		}
		return -1;
	}
	
	public <T>List<T> queryList(String sql,BeanListHandler<T> beanListHandler,Object...obj){
		conn=ConnectionUtil.getConnection();
		try {
			return queryRunner.query(conn, sql,beanListHandler, obj);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ConnectionUtil.clossAll(conn);
		}
		return null;
	}
	
	public <T>T querySingle(String sql,BeanHandler<T> beanHandler,Object...obj){
		conn=ConnectionUtil.getConnection();
		try {
			return queryRunner.query(conn, sql, beanHandler, obj);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ConnectionUtil.clossAll(conn);
		}
		return null;
	}
	
	public Long queryColumn(String sql,ScalarHandler scalarHandler,Object...obj){
		conn=ConnectionUtil.getConnection();
		try {
			return (Long) queryRunner.query(conn, sql, scalarHandler, obj);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ConnectionUtil.clossAll(conn);
		}
		return null;
	}
	
}
